package com.starleague.battletech.unit.mech;

import com.starleague.battletech.BattletechException;
import com.starleague.battletech.unit.HasEngine;
import com.starleague.battletech.unit.HasWeight;

public class MechMovementHelper
{
    // private static final double PRECISION = 0.000001;
    private static final double ROUND_PRECISION = 0.499999;

    private MechMovementHelper()
    {
        // Class can be statically accessed only
    }

    public static <MECH extends HasEngine & HasWeight> int calcWalkMp(MECH mech)
    {
        // Should be an exact match
        return mech.getEngine().getEngineRating() / mech.getWeight().getTonsAsInteger();
    }

    public static <MECH extends HasEngine & HasWeight> int calcRunMp(MECH mech)
    {
        return roundUp(calcWalkMp(mech) * 1.5);
    }

    public static <MECH extends HasEngine & HasWeight & HasMechMovement> int calcMaximumMp(MECH mech)
            throws BattletechException
    {
        switch (mech.getMovementMode())
        {
            case STAND_STILL:
                return 0;

            case WALK:
                return calcWalkMp(mech);

            case RUN:
                return calcRunMp(mech);

            case JUMP:
            default:
                throw new BattletechException("Unsupported MechMovementMode: " + mech.getMovementMode());
        }
    }

    // private int round(double value)
    // {
    // return (int) Math.rint(value);
    // }

    private static int roundUp(double value)
    {
        return (int) Math.rint(value + ROUND_PRECISION);
    }

    // private int roundDown(double value)
    // {
    // return (int) Math.rint(value - ROUND_PRECISION);
    // }
}
